<?php
/*
 * @年糕妈妈
 * @功能说明：系统消息模块
 * @更新说明：暂无更新
 * @文件名 SysmsgController.class.php
 * @编码 UTF-8
 * @创建时间 2015-7-21 下午3:38:37
 * @创建人 XING
 */

namespace dg\Controller;
use dg\Model\SysMsgModel;
use Think\Controller;

class SysmsgController extends Controller {
	/*
	 * 年糕妈妈_系统消息模块
	 * @date: 2015-3-31下午7:20:44
	 * @editor: XING
	 */
	public function index(){
		/*参数初始化*/
		$page = I('get.page') ?: 1;
		$perpage = 20;$url = '';
		$list = M('SysMsg');
		$url = array();
		if(I('get.msgtitle')){
			$list = $list->where('sys_msg_title LIKE "%'.I('get.msgtitle').'%"');
			$url['msgtitle'] = I('get.msgtitle');
		}
		if(I('get.msgid')){
			$list = $list->where(array('sys_msg_id'=>I('get.msgid')));
			$url['msgid'] = I('get.msgid');
		}
		$getObj = clone $list;
		$sysData = $list->order(array('sort'=>'desc'))->page($page,$perpage)->select();
		
		$this->assign('page',setPage($getObj->count(),$perpage,$page,'?'.http_build_query($url)));
		$this->assign('sysData',$sysData);
		$this->display('sysmsg/list');
	}
	
	/*
	 * 年糕妈妈_添加推送
	 * @date: 2015-4-1下午3:04:01
	 * @editor: XING
	 */
	public function addmsg(){
		if(I('post.dosubmit')){
			if(in_array(I('post.ts'),array('type1','type2')) && I('post.voice_url')){
				if(I('post.ts') == 'type1' && !I('post.voice_url')){
					$this->showtips('推广语音文件必须选择');
				}else if(I('post.ts') == 'type2' && !I('post.contents')){
					$this->showtips('文本转语音内容不能为空');
				}
				$getHwares = M('HwcodeEntity')->field('maccode')
						   ->where('cust_id > 0 AND check = 1')->select();
				if($getHwares){
					foreach($getHwares AS $vals){
						switch(I('post.ts')){
							case 'type1':
								$replyWord = filesize($_SERVER['DOCUMENT_ROOT'] . '/../../statics'.I('post.voice_url')).'|'.transUrl(I('post.voice_url'));
								I('post.title') ? $replyWord = '30|'.I('post.title').'|'.$replyWord : '0||'.$replyWord;
								(new \service\imtalk\ImTalk())->sendIm(trim($vals['maccode']), 4, 3, $replyWord);
								break;
							case 'type2':
								$thisTime = time()+1000;$text = trim(I('post.contents'));$randStr = date("YmdHis").rand(10000, 99999);
								$base_path = $_SERVER['DOCUMENT_ROOT'] . '/../../statics/upload/trans/'.$randStr.'.mp3';
								$voiceData = copy('http://api.okvoice.com/tts/?apiKey=e4ecb2444614ba01568bdfd4ec95282b&expires='.$thisTime.'&format=WAV&speed=1&text='.$text.'&voice=cnfemale&signature='.hash_hmac("sha1", 'apiKey=e4ecb2444614ba01568bdfd4ec95282b&expires='.$thisTime.'&format=WAV&speed=1&text='.$text.'&voice=cnfemale', '0d84da8ba463989ad2144c152317055c'), $base_path);
								$sayWords = trim($replyWord);
								if($voiceData){
									$urlData = '/upload/trans/'.$randStr.'.mp3';
									$sayWords = '30|'.trim($replyWord).'|'.filesize($_SERVER['DOCUMENT_ROOT'] . '/../../statics/upload/trans/'.$randStr.'.mp3').'|'.transUrl($urlData);
									(new \service\imtalk\ImTalk())->sendIm(trim($vals['maccode']), 4, 3, $replyWord);
								}
								break;
							case 'type3':
								if(!I('post.texts')){
									$this->showtips('文本内容不能为空');
								}
								$replyWord = '120|'.I('post.texts').'|0|0';
								(new \service\imtalk\ImTalk())->sendIm(trim($vals['maccode']), 4, 3, $replyWord);
								break;
							default:
								break;
						}
					}
				}
				$setInsert = M('SpreadEntity')->add(array(
								'spread_title' => I('post.ts') == 'type1' ? I('post.title') : I('post.contents'),
								'spread_file' => I('post.ts') == 'type1' ? I('post.voice_url') : $urlData,
								'res_id' => getLoginMsg()->rid,
								'add_at' => time()
							));
				if($setInsert){
					$this->showtips('操作成功');
				}else{
					$this->showtips('操作失败');
				}
			}else{
				$this->showtips('非法参数');
			}
			$addId = M('SysMsg')->add(array(
					'sys_msg_title' => I('post.title'),
					'sys_msg_content' => I('post.contents'),
					'sort' => 0,
					'add_at' => time(),
					'update_at' => time(),
					'editor' => getLoginMsg()->rid
				));
			if($addId){
				putlog('添加推送消息 ID`'.$addId.'`','SysmsgController','addmsg');
				$this->showtips('添加推送成功');
			}else{
				$this->showtips('操作失败');
			}
		}
		
		$this->display('sysmsg/addmsg');
	}
	
	/*
	 * 年糕妈妈_推送编辑
	 * @date: 2015-4-1下午3:05:42
	 * @editor: XING
	 */
	public function delemsg(){
		//暂时不允许删除
	}
	
	/*
	 * 年糕妈妈_日志功能
	 * @date: 2015-4-2下午2:09:00
	 * @editor: XING
	 */
	public function showlogs(){
		/*参数初始化*/
		$page = I('get.page') ?: 1;
		$perpage = 10;$url = '';
		$getResList = M('ResEntity')->where(array('is_active'=>'1'))->select();
		$list = M('SysLogs');
		if(I('get.rid')){
			$list = $list->where(array('sys_editor'=>I('get.rid')));
			$url['rid'] = I('get.rid');
		}
		if(I('get.dotime')){
			$list = $list->where('add_at > '.strtotime(I('get.dotime')).' AND add_at < '.(strtotime(I('get.dotime'))+24*3600));
			$url['dotime'] = I('get.dotime');
		}
		
		$getObj = clone $list;
		$sysData = $list->order(array('sys_action_id'=>'desc'))->page($page,$perpage)->select();
		$this->assign('page',setPage($getObj->count(),$perpage,$page,'?'.http_build_query($url)));
		$this->assign('sysData',$sysData);
		$this->assign('getResList',$getResList);
		$this->display('sysmsg/logs');
	}
}